package com.airport.ticket.controller;

import com.airport.ticket.common.BaseController;
import com.airport.ticket.common.DateUtil;
import com.airport.ticket.common.PageInfoUtil;
import com.airport.ticket.entity.Borad;
import com.airport.ticket.entity.BoradQuery;
import com.airport.ticket.service.BoradService;
import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 公告的Controller
 *
 * @author ZjxMi
 */
@Controller
@RequestMapping("/admin")
@Log4j2
public class BoradController extends BaseController {
    @Autowired
    private BoradService boradService;

    /**
     * 公告列表页面
     *
     * @return
     */
    @RequestMapping("/boradList.html")
    public String boradList(HttpServletRequest request, Model model) {
        log.info("--boradList--");
        model.addAttribute("nav", "4");
        model.addAttribute("title", "公告栏");

        /* 分页信息 */
        PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
        /* 搜索条件,生成公共的搜索条件 */
        BoradQuery paramQuery = new BoradQuery();
        /* 查询数据库
         * 所有的数据以JSON的形式返回
         *  */
        List<Borad> dataList = this.boradService.findList(pageInfoUtil, paramQuery);
        /* 将结果存储到Request中 */
        model.addAttribute("dataList", dataList);
        model.addAttribute("pageInfoUtil", pageInfoUtil);
        model.addAttribute("pageNow", pageInfoUtil.getCurrentPage());
        model.addAttribute("pageSize", pageInfoUtil.getPageSize());
        model.addAttribute("totalPage", pageInfoUtil.getTotalPage());
        return "/admin/borad/boradList";
    }

    /**
     * 打开添加公告页面
     *
     * @return
     */
    @RequestMapping("/boradInsert.html")
    public String boradInsert(Model model) {
        log.info("--boradInsert--");
        model.addAttribute("title", "新增公告");
        return "/admin/borad/boradInsert";
    }

    /**
     * 添加公告提交操作
     *
     * @return
     */
    @RequestMapping(value = "/boradInsertSubmit.html")
    public String boradInsertSubmit(String pubTimeStr, Borad data, Model model) {
        log.info("--boradInsertSubmit--");
        model.addAttribute("title", "新增公告");
        /* 为对象赋值 */
        data.setPubTime(new Date());
        data.setCreateTime(new Date());
        data.setUpdateTime(new Date());
        /* 保存结果 */
        int save = this.boradService.save(data);
        log.info("--公告添加返回结果:{}--", save);
        return "redirect:/admin/boradList.html";
    }

    /**
     * 打开更新公告页面
     *
     * @return
     */
    @RequestMapping("/boradUpdate.html")
    public String boradUpdate(String id, String operType, Model model) {
        log.info("--boradUpdate--");
        model.addAttribute("title", "更新公告");
        BoradQuery paramQuery = new BoradQuery();
        Borad entity = paramQuery.getEntity();
        entity.setId(Integer.parseInt(id));
        /* 查询数据库 */
        Borad one = this.boradService.findOne(paramQuery);
        /* 存储request */
        model.addAttribute("one", one);

        if ("update".equalsIgnoreCase(operType)) {
            return "/admin/borad/boradUpdate";
        }
        return "/admin/borad/boradInfo";
    }

    /**
     * 添加公告提交操作
     *
     * @return
     */
    @RequestMapping(value = "/boradUpdateSubmit.html")
    public String boradUpdateSubmit(String id,
                                    HttpServletRequest request, Model model) {
        log.info("--boradUpdateSubmit--");
        BoradQuery paramQuery = new BoradQuery();
        Borad entity = paramQuery.getEntity();
        entity.setId(Integer.parseInt(id));
        /* 查询数据库 */
        Borad one = this.boradService.findOne(paramQuery);

        /* 接收参数 */
        String name = request.getParameter("name");
        String content = request.getParameter("content");
        /* 设置属性 */
        one.setName(name);
        one.setContent(content);

        one.setUpdateTime(new Date());
        /* 数据库操作 */
        int update = boradService.update(one);
        log.info("--公告添加返回结果:{}--", update);
        return "redirect:/admin/boradList.html";
    }

    /**
     * 添加公告提交操作
     *
     * @return
     */
    @RequestMapping(value = "/boradDelete.html")
    public String boradDelete(String id, Model model) {
        log.info("--boradDelete--");
        BoradQuery paramQuery = new BoradQuery();
        Borad entity = paramQuery.getEntity();
        entity.setId(Integer.parseInt(id));
        /* 保存结果 */
        int save = this.boradService.delete(paramQuery);
        log.info("--公告删除返回结果:{}--", save);
        return "redirect:/admin/boradList.html";
    }
}